package se.peterbjorkman.android.trafikkamera;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.DialogInterface.OnClickListener;
import android.content.pm.PackageManager.NameNotFoundException;
import android.net.Uri;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

public class AboutDialogBuilder {
	private static final String LOGTAG = AboutDialogBuilder.class.getName();
	private final Context mContext;

	public AboutDialogBuilder(Context context) {
		mContext = context;
	}

	public Dialog getAboutDialog() {
		final Context context = mContext;
		AlertDialog.Builder d = new AlertDialog.Builder(context);
		d.setTitle(getString(R.string.about) + " " + getString(R.string.app_name) + " " + getVersion());
		d.setIcon(android.R.drawable.ic_menu_info_details);
		d.setPositiveButton(R.string.aboutHomepage, new OnClickListener() {
			public void onClick(DialogInterface dialog, int which) {
				Uri uri = Uri.parse(getString(R.string.aboutHomepageUrl));
				context.startActivity(new Intent(Intent.ACTION_VIEW, uri));
			}
		});
		// d.setNeutralButton(R.string.aboutDonation, new OnClickListener() {
		// public void onClick(DialogInterface arg0, int arg1) {
		// Uri uri = Uri.parse(getString(R.string.aboutDonationUrl));
		// startActivity(new Intent(Intent.ACTION_VIEW, uri));
		// }
		// });
		d.setNegativeButton(getString(R.string.closeString), null);
		View v = LayoutInflater.from(context).inflate(R.layout.about, null);

		final int size = 12;

		TextView generalTextView = (TextView) v.findViewById(R.id.AboutText);
		generalTextView.setText(getString(R.string.aboutTextGeneral));
		generalTextView.setTextSize(size);

		InfoBlock blockSource = (InfoBlock) v
				.findViewById(R.id.InfoBlockSource);
		blockSource.setTitleText(getString(R.string.aboutTitleSource));
		blockSource.setInfoText(getString(R.string.aboutTextSource));
		blockSource.SetInfoTextSize(size);

		InfoBlock blockLicense = (InfoBlock) v
				.findViewById(R.id.InfoBlockDeveloper);
		blockLicense.setTitleText(getString(R.string.aboutTitleLicense));
		String text = getString(R.string.aboutTextLicenseIntro) + "\n";
		blockLicense.setInfoText(text);
		blockLicense.SetInfoTextSize(size);
		blockLicense.setUseLinks(false);

		text = getString(R.string.aboutTextLicenseSource);
		blockLicense.addExtraText(text, context, false);

		text = getString(R.string.aboutTextLicenseCopyright);
		blockLicense.addExtraText(text, context, false);

		text = getString(R.string.aboutTextLicenseGPLNoWarranty);
		blockLicense.addExtraText(text, context, false);

		text = getString(R.string.aboutTextLicenseGPLDistribute);
		blockLicense.addExtraText(text, context, false);

		text = getString(R.string.aboutTextLicenseGPLDetails);
		blockLicense.addExtraText(text, context, false);

		text = getString(R.string.aboutTextLicenseGPLDetailsURL);
		blockLicense.addExtraText(text, context, true);
        
		d.setView(v);

		Log.d(LOGTAG, "Returning from onCreateDialog");

		return d.create();
	}

	private String getVersion() {
		Log.d(LOGTAG, "In getVersion");
		try {
			return mContext.getPackageManager().getPackageInfo(
					mContext.getPackageName(), 0).versionName;
		} catch (NameNotFoundException e) {
			Log.e(LOGTAG, "NameNotFoundException in  getVersion "
					+ e.getLocalizedMessage());
			// Ignore.
		}
		Log.w(LOGTAG, "Returning empty string from getVersion");
		return "";
	}

	private String getString(int resId) {
		return mContext.getString(resId);
	}
}
